package com.cblz.bd.server.core.controller;

import com.cblz.bd.common.core.entity.BaAdmin;
import com.cblz.bd.common.core.entity.system.QueryRequest;
import com.cblz.bd.common.core.utils.ResultResponse;
import com.cblz.bd.common.core.utils.SearchParamsUtil;
import com.cblz.bd.server.core.annotation.AdminApiController;
import com.cblz.bd.server.core.entity.dto.BaAdminDTO;
import com.cblz.bd.server.core.entity.vo.BaAdminManagerVO;
import com.cblz.bd.server.core.service.BaAdminService;
import io.vavr.Tuple4;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * {@code @Name:} BaAdminManagerController
 * {@code @Description: } 后台管理用户控制层
 * {@code @Author:} by yanjieping
 * {@code @CreateDate:} 2022/11/11 - 周五
 */
@AdminApiController("admin")
@RequiredArgsConstructor
public class BaAdminManagerController {

    private final BaAdminService baAdminService;

    @PostMapping("index")
    public ResultResponse list(@RequestBody Map<String, Object> searchParams) throws Exception {
        var queryRequest = new QueryRequest();

        Tuple4 paramsHandle = SearchParamsUtil.searchParamsHandle(searchParams);
        var search = (List<LinkedHashMap<String, Object>>) paramsHandle._1;
        var page = paramsHandle._2;
        var limit = paramsHandle._3;
        var quickSearch = (Map<String, String>) paramsHandle._4;

        if (page != null)
            queryRequest.setPageNum(Integer.parseInt(page.toString()));
        if (limit != null)
            queryRequest.setPageSize(Integer.parseInt(limit.toString()));

        BaAdmin baAdmin = SearchParamsUtil.ParamsFormat2Class(new BaAdmin(), search, quickSearch);

        Map<String, Object> allUserInfo = baAdminService.getAllUserInfo(baAdmin, queryRequest);
        if (allUserInfo == null)
            return new ResultResponse().message("后台管理员用户数据为空");

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", allUserInfo.get("allinfo"));
        resultMap.put("total", allUserInfo.get("total"));
        resultMap.put("remark", "");
        return new ResultResponse().data(resultMap);
    }

    @GetMapping("edit")
    public ResultResponse getById(@RequestParam("id") Integer id) {
        BaAdminManagerVO baAdminManagerVo = baAdminService.getById(id);
        return new ResultResponse().data(baAdminManagerVo);
    }

    @PostMapping("add")
    public ResultResponse add(@RequestBody BaAdminDTO baAdmin) {
        baAdminService.add(baAdmin);
        return new ResultResponse().message("添加成功");
    }

    @DeleteMapping("del")
    public ResultResponse delete(@RequestBody Map Ids) {
        List<Integer> ids = (List) Ids.get("ids");
        baAdminService.deleteByIds(ids);
        return new ResultResponse().message("删除成功");
    }

    @PostMapping("edit")
    public ResultResponse edit(@RequestBody BaAdminDTO baAdmin) {
        baAdminService.edit(baAdmin);
        return new ResultResponse().message("修改成功");
    }
}
